package com.luyuan.crm.member.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.model.OnlineMember;
import com.luyuan.crm.member.service.OnlineMemberService;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.components.security.model.Role;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.foundation.security.AppUserContext;

@Component("onlineMemberAction")
@Scope("view")
public class OnlineMemberAction extends BaseAction {

    @Resource(name = "onlineMemberService")
    private OnlineMemberService onlineMemberService;

    private OnlineMember criteria = new OnlineMember();

    // 添加或编辑模型
    private OnlineMember memberModel = new OnlineMember();

    public OnlineMember getMemberModel() {
        return memberModel;
    }

    public OnlineMember getCriteria() {
        return criteria;
    }

    public DataModel getTableModel() {
        CriteriaTemplate c = CriteriaTemplate.forClass(OnlineMember.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        AppUser appUser = (AppUser) AppUserContext.getCurrentUser();
        if (!isCM(appUser)) {
            c.createCriteria("member").createCriteria("support").add(r.eq("appUser", appUser));
        }
        TablePage tablePage = TableContext.getTablePage();
        if (tablePage == null) {
            return new PageDataModel(new Page());
        }
        c.add(r.eq("onlineStatus", Boolean.TRUE));
        List<Order> orders = new ArrayList<Order>();
        orders.add(Order.desc("loginTime"));
        Page page = onlineMemberService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    @SuppressWarnings("unchecked")
    public List<Member> getLastestMember() {
        CriteriaTemplate c = CriteriaTemplate.forClass(OnlineMember.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        AppUser appUser = (AppUser) AppUserContext.getCurrentUser();
        if (!isCM(appUser)) {
            c.createCriteria("member").createCriteria("support").add(r.eq("appUser", appUser));
        }
        c.add(r.eq("onlineStatus", Boolean.TRUE));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, -10);
        Date sDate = calendar.getTime();
        c.add(r.gt("loginTime", sDate));

        List<Order> orders = new ArrayList<Order>();
        orders.add(Order.desc("loginTime"));
        Page page = onlineMemberService.pagedQuery(c, 1, 10, orders);
        return (List<Member>) page.getResult();
    }

    private boolean isCM(AppUser appUser) {
        Set<Role> roles = appUser.getRoles();
        for (Role role : roles) {
            if (role.getRoleName().contains("经理")) {
                return true;
            }
        }
        return false;
    }

    public void setOnlineMemberService(OnlineMemberService onlineMemberService) {
        this.onlineMemberService = onlineMemberService;
    }

}
